
<%@page import="storeMovie.JavaBean.Movie"%>
<%@page import="java.util.List"%>
<%@page import="storeMovie.Util.MovieManager"%>
<%@page import="java.sql.*"%>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1" import="java.util.Vector"%>
<%
	MovieManager manager = new MovieManager();
%>
<%
	Vector<Integer> cart = (Vector<Integer>) session
			.getAttribute("shopingcart");
	Vector<Integer> cartCount = (Vector<Integer>) session
			.getAttribute("cartCount");

	if (cart == null) {
		cart = new Vector<Integer>();
		session.setAttribute("shopingcart", cart);
	}
	if (cartCount == null) {
		cartCount = new Vector<Integer>();
		session.setAttribute("cartCount", cartCount);
	}
	String option = (String) request.getParameter("option");
	String id = (String) request.getParameter("movieID");
	String name = (String) session.getAttribute("userName");
	if (("clear").equals(option)) {
		cart.removeAllElements();
		cartCount.removeAllElements();
	} else if (("delete").equals(option)) {
		System.out.println("Delete");
		String deleteId = (String) request.getParameter("deletemovie");
		Integer movieID = new Integer(Integer.parseInt(deleteId));
		int index = cart.indexOf(movieID);
		cart.remove(movieID);
		cartCount.removeElementAt(index);
	} else if (("update").equals(option)) {

	}

	if (name == null) {
		out.println("<h1>You must Login to buy our product</h1>");
	} else {
		if (id != null) {
			int movieID = Integer.parseInt(id);
			if (!cart.contains(movieID)) {
				cart.add(new Integer(movieID));
				session.setAttribute("shopingcart", cart);
				cartCount.add(new Integer(1));
				session.setAttribute("cartCount", cartCount);
			}
		} else {
			if (cart.size() == 0) {
				out
						.println("<h1 style='text-align:center;'>You don't chose any product</h1>");
			} else {
				out
						.println("<h1 style='text-align:center;'>Product you buy: </h1><br>");
				out.println("<p>");
				out
						.println("<a href='#' onclick='purchase()'>Purchase</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
				out
						.println("<a href='#' onclick='clearShop()'>Clear ShoppingCart</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
				out.println("<a href='Main.jsp'>Continue Shopping</a>");
				out
						.println("<form method='post'  id='myform' name='myform'>");

				out
						.println("<table width=100%' border='0' cellspacing='0' cellpadding='0' id='tableOrder'>");
				out.println("<tr align='center' bgcolor='red'>");
				out
						.println("<td width='40%'><strong>Product Name</strong></td>");
				out
						.println("<td width='20%'><strong>Price</strong></td>");
				out
						.println("<td width='10%'><strong>Quantity</strong></td>");
				out
						.println("<td width='20%'><strong>Total </strong></td>");
				out.println("<td></td>");
				out.println("</tr>");
				int totalCost = 0;

				for (int i = 0; i < cart.size(); i++) {
					int idMovie = cart.get(i);
					Movie movie = manager.getMovie(idMovie);
					int quantity = cartCount.get(i);
					if (i % 2 == 0) {
						out.println("<tr  align='center'>");
					} else {
						out
								.println("<tr align='center' bgcolor=#efefef>");
					}
					out
							.println("<td>" + movie.getMovieName()
									+ "</td>");
					out.println("<td>" + movie.getMoviePrice()
							+ "$</td>");
					out.println("<td><input id='qty_"
							+ movie.getMovieId()
							+ "' type='text'name='qty_"
							+ movie.getMovieId() + "' value='"
							+ quantity + "'/></td>");
					int price = movie.getMoviePrice();
					int total = price * quantity;
					totalCost += total;
					out.println("<td>" + total + "$</td>");
					out.println("<td><a href='#' onclick='deleteMovie("
							+ movie.getMovieId()

							+ ")'>Delete</a></td>");
					out.println("</tr>");
				}
				out
						.println("<tr><td align='right' colspan='7' ><span style='color: red;padding-right:61px;'><strong>Total Cost:   "
								+ totalCost
								+ "$</strong></span></td></tr>");
				out.println("<tr>");
				out
						.println("<td align='center' colspan='7'><input type='button' onClick='updateCart(this.form)' value='Update Shopping Cart'/></td>");
				out.println("</tr>");
				out.println("</table>");
				out.println("</form>");
			}
		}
	}
%>